<?php

namespace App\Http\Controllers;

use App\Http\Model\Api\AccompanyUser;
use App\Http\Model\Api\FinanceRechargeOrder;
use App\Http\Model\Config;
use App\Http\Model\OutJson;
use App\Http\Model\TopupRebateRecharge;
use App\Http\Model\User;
use App\Http\Services\ConfigServices;
use App\Http\Services\ShareDiamondServices;
use App\Http\Services\ShareSendGoodsServices;
use App\Http\Services\UserServices;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

/*
 * 充值赠送钻石
 * */

class WhoIsOrderKingController extends BaseController
{
    protected $model;
    protected $configModel;


    public function __construct(AccompanyUser $accompanyUser, Config $config)
    {
        $this->model = $accompanyUser;
        $this->configModel = $config;
    }

    public function beforeThirty(Request $request) {
        $times = $this->effectiveTime();
        $begin = $times[0];
        $end = $times[1];
        $list = $this->model->beforeThrity($begin, $end);

        // 处理头像
        foreach($list as $id => &$user) {
            $user['portrait'] = build_image(json_decode($user['portrait'])[0]);
        }
        unset($user);
        OutJson::toJson(200, '成功', $list);
    }

    public function userInfo(Request $request, $id) {
        $times = $this->effectiveTime();
        $begin = $times[0];
        $end = $times[1];
        $list = $this->model->userInfo($id, $begin, $end);

        // 处理头像
        foreach($list as $id => &$user) {
            $user['portrait'] = build_image(json_decode($user['portrait'])[0]);
        }
        unset($user);
        OutJson::toJson(200, '成功', $list);
    }

    public function effectiveTime() {
        $configs = $this->configModel->getShareGoodsConfig(16);
        $times = $configs['order_king']['value'];

        return explode(' - ', $times);   
    }

    public function provideDiamond() {
        $times = $this->effectiveTime();
        $begin = $times[0];
        $end = $times[1];
        $list = $this->model->provideDiamond($begin, $end);

        foreach ($list as $key => $value) {
            $this->sendMsg($value->user_id, $value->total_price / 100 * 0.05);
        }

        OutJson::toJson(200,'1111', $list);
    }

    public function sendMsg($user_id, $prize) {
        //发送接口请求
        $recharge = [
            'user_id' => $user_id,
            'diamond_num' => $prize,
            'msg' => '谁是接单王',
        ];
        $diamond_api = json_decode(curl_request(env('KK_API_HOST') . '/finance/diamond/recharge', $recharge), true);
        if ($diamond_api['code'] != 0) {
            //充值失败 销毁队列
            write_log('orderKingConsumer', 'oneUserRechargeError', json_encode($diamond_api));
            return false;
        }

        // 发送消息
        $msg_data_one['user_id'] = $user_id;
        $msg_data_one['msg'] = "谁是接单王奖励 钻石* " . $prize;
        curl_request(env('KK_API_HOST') . '/msg/text', $msg_data_one);

        write_log('orderKingConsumer', '谁是接单王', $user_id);
    }
}
